package com.mflex.WHSmartWhouseAd.adapter;

import android.annotation.SuppressLint;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.listener.OnRecyclerViewItemClickListener;
import com.mflex.WHSmartWhouseAd.model.MenuModel;

import java.util.List;

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.ViewHolder> implements View.OnClickListener {
    //声明接口，用于点击item
    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    private List<MenuModel> mMenuModelList;

    @Override
    public void onClick(View view) {

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mImage;
        TextView mName;

        public ViewHolder(View view) {
            super(view);
            mImage = view.findViewById(R.id.image);
            mName = view.findViewById(R.id.name);
        }

    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }


    public MenuAdapter(List<MenuModel> menuModelList) {
        mMenuModelList = menuModelList;
    }

    @Override

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        view.setOnClickListener(this);

        //这三行代码实现点击有水波纹效果
        TypedValue typedValue = new TypedValue();
        parent.getContext().getTheme().resolveAttribute(com.king.zxing.R.attr.selectableItemBackground, typedValue, true);
        view.setBackgroundResource(typedValue.resourceId);

        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") final int position) {

        MenuModel menuModel = mMenuModelList.get(position);
        holder.mImage.setImageResource(menuModel.getImageId());
        holder.mName.setText(menuModel.getName());

        //实现点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onRecyclerViewItemClickListener != null) {
                    onRecyclerViewItemClickListener.onItemClick(view, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mMenuModelList.size();
    }
}
